﻿using BySoft.Server.Business;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Web.Controls;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ProductStock
{
    public partial class DeliveryOrderList : AuthBasePage
    {
        
        #region 变量定义
        int m_count = 0;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
                BindData(0);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        void InitData()
        {
            //BindParameterForListControl("ProcessType", ddlProcessType);
            //UIHelper.InsertFirstItem(ddlProcessType);
        }

        private void BindData(int pageIndex)
        {
            string strFrom = CommonMethod.GetStartTime(cpFromDateS.Value);
            string strTo = CommonMethod.GetEndTime(cpEndDateS.Value);
            DataTable dt = BsDeliveryorder.SelectList(txtBillID.Text.Trim(),txtProductID.Text.Trim(), txtWorkOrderNo.Text.Trim(),  strFrom, strTo,
                                                        pageIndex, pcData.PageSize, out m_count);

            if (dt.Rows.Count > 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();

                  UIHelper.GroupRows(grdResult, "lblBillID", new int[] { 0, 1,  2,8, 9, 10 });
               pcData.CurrentIndex = pageIndex;
               }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.UpdatePanel1, "iframeAutoFit();");
        }

        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            BindData(e.PageIndex);
        }

        /// <summary>
        ///   行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCode = e.CommandArgument.ToString();
            strCode = HttpUtility.UrlEncode(strCode.Trim());
            switch (e.CommandName)
            {
                case "viewdata":
                    strCode = HttpUtility.UrlEncode(strCode);
                    Response.Redirect("DeliveryOrderView.aspx?id=" + strCode);
                    break;
                case "edi":
                   
                    if (BsOutbackmat.CheckUsed(strCode))
                    {
                        UIHelper.AlertMessage(this.Page, "已经生成后置单据,不能编辑");
                        return;
                    }
                    Response.Redirect("DeliveryOrderEdit.aspx?id=" + strCode);
                    break;
                case "del":
                    if (BsOutbackmat.CheckUsed(strCode))
                    {
                        UIHelper.AlertMessage(this.Page, "已经生成后置单据,不能删除");
                        return;
                    }
                    BsDeliveryorder.Del(strCode);
                    BindData(0);
                    UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
                    break;
                //case "Check":
                //    Response.Redirect("SubpackageCheck.aspx?id=" + strCode);
                //    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton ibtnEdi = (ImageButton)e.Row.FindControl("ibtnEdi");
                ImageButton lbtnCheck = (ImageButton)e.Row.FindControl("lbtnCheck");
                #region 删除绑定
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
                #endregion 
            }
        }
    }
}